package com.bot.edu.interviewmanagement.resource.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bot.edu.interviewmanagement.interview.entity.EmploymenInterview;
import com.bot.edu.interviewmanagement.interview.mapper.EmploymenInterviewMapper;
import com.bot.edu.interviewmanagement.interview.service.IEmploymenInterviewService;
import com.bot.edu.utils.SnowFlake;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import com.bot.edu.interviewmanagement.resource.entity.FeedbacResource;
import com.bot.edu.interviewmanagement.resource.service.IFeedbacResourceService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 反馈资源表
 * @Author: jeecg-boot
 * @Date:   2022-04-09
 * @Version: V1.0
 */
@Api(tags="反馈资源表")
@RestController
@RequestMapping("/resource/feedbacResource")
@Slf4j
public class FeedbacResourceController extends JeecgController<FeedbacResource, IFeedbacResourceService> {
	@Autowired
	private IFeedbacResourceService feedbacResourceService;

	@Autowired
	private IEmploymenInterviewService employmenInterviewService;
	
	/**
	 * 分页列表查询
	 *
	 * @param feedbacResource
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "反馈资源表-分页列表查询")
	@ApiOperation(value="反馈资源表-分页列表查询", notes="反馈资源表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<FeedbacResource>> queryPageList(FeedbacResource feedbacResource,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<FeedbacResource> queryWrapper = QueryGenerator.initQueryWrapper(feedbacResource, req.getParameterMap());
		Page<FeedbacResource> page = new Page<FeedbacResource>(pageNo, pageSize);
		IPage<FeedbacResource> pageList = feedbacResourceService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param feedbacResource
	 * @return
	 */
	@AutoLog(value = "反馈资源表-添加")
	@ApiOperation(value="反馈资源表-添加", notes="反馈资源表-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody FeedbacResource feedbacResource) {
		feedbacResourceService.save(feedbacResource);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param feedbacResource
	 * @return
	 */
	@AutoLog(value = "反馈资源表-编辑")
	@ApiOperation(value="反馈资源表-编辑", notes="反馈资源表-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody FeedbacResource feedbacResource) {
		feedbacResourceService.updateById(feedbacResource);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "反馈资源表-通过id删除")
	@ApiOperation(value="反馈资源表-通过id删除", notes="反馈资源表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		feedbacResourceService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "反馈资源表-批量删除")
	@ApiOperation(value="反馈资源表-批量删除", notes="反馈资源表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.feedbacResourceService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "反馈资源表-通过id查询")
	@ApiOperation(value="反馈资源表-通过id查询", notes="反馈资源表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<FeedbacResource> queryById(@RequestParam(name="id",required=true) String id) {
		FeedbacResource feedbacResource = feedbacResourceService.getById(id);
		if(feedbacResource==null) {
			return Result.error("未找到对应数据",null);
		}
		return Result.OK(feedbacResource);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param feedbacResource
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, FeedbacResource feedbacResource) {
        return super.exportXls(request, feedbacResource, FeedbacResource.class, "反馈资源表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, FeedbacResource.class);
    }

	 /**
	  *   添加面试反馈
	  *
	  * @param feedbacResource
	  * @return
	  */
	 @PostMapping(value = "/addResource")
	 public Result<String> addResource(@RequestBody FeedbacResource feedbacResource) {
	 	//随机数生成反馈编号
	 	feedbacResource.setResourceId(SnowFlake.getId());
	 	//反馈提交时间
		 DateTime dateTime = new DateTime();
		 feedbacResource.setResourceTime(dateTime);

		 EmploymenInterview employmenInterview = new EmploymenInterview();
		 employmenInterview.setInterviewId(feedbacResource.getInterviewId());
		 employmenInterview.setInterviewResults(feedbacResource.getInterviewResults());
		 UpdateWrapper<EmploymenInterview> updateWrapper = new UpdateWrapper<>();
		 updateWrapper.lambda().eq(EmploymenInterview::getInterviewId,employmenInterview.getInterviewId());
		 boolean update = employmenInterviewService.update(employmenInterview, updateWrapper);
		 boolean save = feedbacResourceService.save(feedbacResource);
		 if (save==true&&update==true){
			 return Result.OK("添加成功！");
		 }
		 return Result.error("添加失败！");
	 }

	 /**
	  *   修改面试反馈
	  *
	  * @param feedbacResource
	  * @return
	  */
	 @PostMapping(value = "/upResource")
	 public Result<String> upResource(@RequestBody FeedbacResource feedbacResource) {
		 EmploymenInterview employmenInterview = new EmploymenInterview();
		 employmenInterview.setInterviewId(feedbacResource.getInterviewId());
		 employmenInterview.setInterviewResults(feedbacResource.getInterviewResults());
		 UpdateWrapper<EmploymenInterview> updateWrapper = new UpdateWrapper<>();
		 updateWrapper.lambda().eq(EmploymenInterview::getInterviewId,employmenInterview.getInterviewId());
		 boolean update = employmenInterviewService.update(employmenInterview, updateWrapper);
		 boolean i = feedbacResourceService.updateById(feedbacResource);
		 if (i==true&&update==true){
			 return Result.OK("修改成功！");
		 }
		 return Result.error("修改失败！");
	 }

	 /**
	  * 通过面试预约编号id查询
	  *
	  * @param interviewId
	  * @return
	  */
	 @GetMapping(value = "/queryByInterviewId")
	 public Result<FeedbacResource> queryByInterviewId(@RequestParam(name="interviewId",required=true) String interviewId) {
		 QueryWrapper<FeedbacResource> queryWrapper = new QueryWrapper<>();
		 queryWrapper.lambda().eq(FeedbacResource::getInterviewId,interviewId);
		 FeedbacResource feedbacResource = feedbacResourceService.getOne(queryWrapper);
		 if(feedbacResource==null) {
			 return Result.error("未找到对应数据",null);
		 }
		 return Result.OK(feedbacResource);
	 }

}
